#!/usr/bin/env bash

set -eu
pushd "$(dirname "${BASH_SOURCE[0]}")"/../..

ORPHAN_ASDF=()
mapfile ORPHAN_ASDF < <(find "$HOME/.asdf/installs/" -maxdepth 2 -empty)

for dir in "${ORPHAN_ASDF[@]}"; do
    echo "Removing orphaned .asdf directory: ${dir}"
    rm -rf "${dir}"
done

TOOL_VERSION_FILES=()
mapfile -d $'\0' TOOL_VERSION_FILES < <(fd .tool-versions --hidden --absolute-path --print0)

for file in "${TOOL_VERSION_FILES[@]}"; do
    echo "Installing asdf dependencies as defined in ${file}:"
    parent=$(dirname "${file}")
    pushd "${parent}"

    asdf install

    popd
done

popd
